﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;

using Ample.Model;

namespace Ample.DAL
{
	public class OrderManager : DBBase
    {
        public static OrderManager Create()
        {
            return new OrderManager();
        }

        #region Select Methods

        public IEnumerable<Order> GetProducts()
        {
            using (AmpleDataContext context = DataContext)
            {
                return context.AMPLE_Orders_SelectManual().ToArray<Order>();
            }
        }

        public Order GetOrderById(int orderId)
        {
            using (AmpleDataContext context = DataContext)
            {
                return context.AMPLE_Orders_Select_By_OrderIdManual(orderId).SingleOrDefault<Product>();
            }
        }

        #endregion

        #region Update Methods

        public OperationStatus UpdateOrder(Order order)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.AMPLE_Orders_Update(
                   prod.ProductId,
                   prod.ProductTypeId,
                   prod.Name,
                   prod.Price,
                   prod.Description,
                   prod.Package
                   );
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }


        #endregion

        #region Insert Methods

        public OperationStatus InsertOrder(Order order )
        {
            AmpleDataContext context = null;
            int ? newprodId = null;
            try
            {
                context = DataContext;
                var newprod = context.AMPLE_Orders_Insert(
                   prod.ProductTypeId,
                   prod.Name,
                   prod.Price,
                   prod.Description,
                   prod.Package
                   );

                newprodId = newprod.ReturnValue as int?;
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null, newprodId.Value);
        }

        #endregion

        #region Delete Methods

        public OperationStatus DeleteOrder(Order order)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.AMPLE_Orders_Delete(order.OrderId);
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }

        #endregion

    }
}
